package com.link.play.fit.ai.utils


import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import com.link.play.fit.ai.utils.AppX.context


/**
 * description：
 */
class ScreenUtil {
    companion object{
        /**
         * 获得屏幕高度
         */
        fun getScreenWidth(): Int {
            val wm = context
                    .getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val outMetrics = DisplayMetrics()
            wm.defaultDisplay.getMetrics(outMetrics)
            return outMetrics.widthPixels
        }

        /**
         * 获得屏幕宽度
         */
        fun getScreenHeight(): Int {
            val wm = context
                    .getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val outMetrics = DisplayMetrics()
            wm.defaultDisplay.getMetrics(outMetrics)
            return outMetrics.heightPixels
        }
        fun pxToDp( px: Float): Float {
            val scale = context.resources.displayMetrics.density
            return px / scale
        }

        fun dpToPx( dp: Float): Float {
            val scale = context.resources.displayMetrics.density
            return (dp * scale + 0.5f)
        }

        fun getWidthPixels(context: Activity):Int{
            val displayMetrics = DisplayMetrics()
            context.windowManager.defaultDisplay.getMetrics(displayMetrics)
            val width = displayMetrics.widthPixels  // 屏幕宽度（像素）
            val height = displayMetrics.heightPixels  // 屏幕高度（像素）‌:ml-citation{ref="1,3" data="citationList"}
            return width
        }

        fun getHeightPixels(context: Activity):Int{
            val displayMetrics = DisplayMetrics()
            context.windowManager.defaultDisplay.getMetrics(displayMetrics)
            val width = displayMetrics.widthPixels  // 屏幕宽度（像素）
            val height = displayMetrics.heightPixels  // 屏幕高度（像素）‌:ml-citation{ref="1,3" data="citationList"}
            return height
        }

        // 确保获取的是包含系统装饰的完整屏幕高度
        fun getScreenHeightByMetrics(context: Context): Int {
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                windowManager.currentWindowMetrics.bounds.height()
            } else {
                val metrics = DisplayMetrics()
                windowManager.defaultDisplay.getRealMetrics(metrics)
                metrics.heightPixels
            }
        }

        fun getScreenHeightBySource(context: Context): Int {
            val screenHeight = context.resources.displayMetrics.heightPixels
            return screenHeight
        }
    }


}